package com.hoshiibuy.sku.service.impl;

import com.hoshiibuy.common.rpc.Response;
import com.hoshiibuy.sku.entity.BaseSku;
import com.hoshiibuy.sku.entity.ExtSku;
import com.hoshiibuy.sku.feign.SpuService;
import com.hoshiibuy.sku.mapper.ExtSkuMapper;
import com.hoshiibuy.sku.service.BaseSkuService;
import com.hoshiibuy.sku.service.ExtSkuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hoshiibuy.common.aop.Logable;
import com.hoshiibuy.common.exception.CommonError;
import com.hoshiibuy.common.exception.ServiceException;
import com.hoshiibuy.sku.feign.IdGeneratorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.time.LocalDateTime;

/**
 * <p>
 * 扩展商品表SKU 服务实现类
 * </p>
 *
 * @author zhangshuhu
 * @since 2019-05-05
 */
@Service
public class ExtSkuServiceImpl extends ServiceImpl<ExtSkuMapper, ExtSku> implements ExtSkuService {

    @Autowired
    private IdGeneratorService idGeneratorService;
    @Autowired
    private SpuService spuService;
    @Autowired
    private BaseSkuService baseSkuService;

    /**
    * @param id
    * @return com.hoshiibuy.sku.entity.ExtSku
    * @description: 根据id查询扩展商品表SKU
    */
    @Cacheable(cacheNames = "ExtSku", key = "#root.targetClass.simpleName+':'+#root.methodName+':'+#id")
    @Logable(authName = "SKU管理-扩展商品表SKU -查询扩展商品表SKU ")
    public ExtSku getById(Long id) {
        return baseMapper.selectById(id);
    }


    /**
    * @param entity
    * @return boolean
    * @description: 新增扩展商品表SKU
    */
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "ExtSku", allEntries = true)
    @Logable(authName = "SKU管理-扩展商品表SKU-新增扩展商品表SKU")
    public boolean save(ExtSku entity) {
        if (entity.getSpuid()==null) {
            throw new ServiceException(CommonError.error("新增失败,SpuId不能为空"));
        }
        if (entity.getSkuid()==null) {
            throw new ServiceException(CommonError.error("新增失败,基础SkuId不能为空"));
        }
        Response response=spuService.getExtSpu(entity.getSpuid());
        if(response==null||response.getCode()!=200||response.getData()==null){
            throw new ServiceException(CommonError.error("新增失败，没有找到对应的扩展SPU"));
        }
        BaseSku baseSku =baseSkuService.getById(entity.getSkuid());
        if(baseSku==null){
            throw new ServiceException(CommonError.error("新增失败，没有找到对应的基础Sku"));
        }
        //供应商检验
        //todo
        String id = idGeneratorService.queryGeneratorId();
        if (id == null || id == "") {
            throw new ServiceException(CommonError.error("新增失败"));
        }
        entity.setId(Long.parseLong(id));
        entity.setCreatedtime(LocalDateTime.now());
        int row = baseMapper.insert(entity);
        if (row <= 0) {
            throw new ServiceException(CommonError.error("新增失败"));
        }
        return true;
    }

    /**
    * @param entity
    * @return boolean
    * @description: 修改扩展商品表SKU
    */
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "ExtSku", allEntries = true)
    @Logable(authName = "SKU管理-扩展商品表SKU管理-修改扩展商品表SKU")
    public boolean updateById(ExtSku entity) {

        if(entity.getId()==null){
            throw new ServiceException(CommonError.error("修改失败,主键ID不能为空"));
        }
        if (entity.getSpuid()==null) {
            throw new ServiceException(CommonError.error("修改失败,SpuId不能为空"));
        }
        if (entity.getSkuid()==null) {
            throw new ServiceException(CommonError.error("修改失败,基础SkuId不能为空"));
        }
        Response response=spuService.getExtSpu(entity.getSpuid());
        if(response==null||response.getCode()!=200||response.getData()==null){
            throw new ServiceException(CommonError.error("修改失败，没有找到对应的扩展SPU"));
        }
        BaseSku baseSku =baseSkuService.getById(entity.getSkuid());
        if(baseSku==null){
            throw new ServiceException(CommonError.error("修改失败，没有找到对应的基础Sku"));
        }
        //供应商检验
        //todo
        entity.setUpdatedtime(LocalDateTime.now());
        int row = baseMapper.updateById(entity);
        if (row <= 0) {
            throw new ServiceException(CommonError.error("修改失败"));
        }
        return true;
    }

    /**
    * @param queryWrapper
    * @return IPage
    * @description: 分页查询扩展商品表SKU
    */
    @Logable(authName = "SKU管理-扩展商品表SKU管理-分页查询扩展商品表SKU")
    @Cacheable(cacheNames = "ExtSku", key = "T(com.hoshiibuy.common.utils.DigestUtils).md5('Page(current=' + #page.getCurrent() + ', size=' + #page.getSize() + '), Wrapper(SqlSelect=' + #queryWrapper.getSqlSelect() + ', SqlSegment=' + #queryWrapper.getSqlSegment() + ')')")
    public IPage<ExtSku> page(IPage<ExtSku> page, Wrapper<ExtSku> queryWrapper) {
        return baseMapper.selectPage(page, queryWrapper);
    }
}
